package wtc.stu.dao;

import wtc.stu.entity.StuEntity;
import wtc.stu.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StuDaoStu {
    private DBConnection dbc;
    private Connection conn = null;
    private ResultSet rs = null;
    private PreparedStatement pre = null;
    public StuDaoStu()
    {
        dbc = new DBConnection();

    }
    // 全查询学生信息
    public List<StuEntity> getAll()
    {
        List<StuEntity> ar = new ArrayList<StuEntity>();
        try
        {
            conn = dbc.getConnection();
            String sql = "select * from stuinfo";
            pre = conn.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next())
            {
                StuEntity stu = new StuEntity();
                stu.setSid(rs.getInt("sid"));
                stu.setSname(rs.getString("sname"));
                stu.setAge(rs.getInt("age"));
                stu.setCid(rs.getInt("cid"));
                stu.setSremark(rs.getString("sremark"));
                ar.add(stu);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return ar;
    }
    // 查询一个学生信息表对象
    public StuEntity getOneById(int sid)
    {
        StuEntity stu = new StuEntity();

        try
        {
            conn = dbc.getConnection();
            String sql = "select * from stuinfo where sid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, sid);
            rs = pre.executeQuery();
            while (rs.next())
            {
                stu.setSid(rs.getInt("sid"));
                stu.setSname(rs.getString("sname"));
                stu.setAge(rs.getInt("age"));
                stu.setCid(rs.getInt("cid"));
                stu.setSremark(rs.getString("sremark"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return stu;
    }
    // 增加学生信息表对象
    public void addStu(StuEntity stu)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "insert into stuinfo(sname,age,cid,sremark) values(?,?,?,?)";
            pre = conn.prepareStatement(sql);
            pre.setString(1,stu.getSname());
            pre.setInt(2,stu.getAge());
            pre.setInt(3, stu.getCid());
            pre.setString(4, stu.getSremark());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
    // 修改学生信息表对象
    public void updateStu(StuEntity stu)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "update  stuinfo  set sname=?, age=?, cid=?, sremark=? where sid=?";
            pre = conn.prepareStatement(sql);
            pre.setString(1, stu.getSname());
            pre.setInt(2, stu.getAge());
            pre.setInt(3, stu.getCid());
            pre.setString(4, stu.getSremark());
            pre.setInt(5, stu.getSid());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
    // 删除学生信息表对象
    public void delStu(int sid)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "delete from stuinfo where sid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1,sid);
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
}
